package main

import (
	"demo/models"
	_ "demo/routers"
	"encoding/gob"
	"fmt"
	"github.com/astaxie/beego/orm"
	"github.com/astaxie/beego/session"
	_ "github.com/astaxie/beego/session/redis"
	"time"

	"github.com/astaxie/beego"
)

func init(){
	initDb()
	initSession()
}

func initDb()  {
	orm.RegisterDriver("mysql", orm.DRMySQL)
	//orm.RegisterDataBase("default", "mysql", "root:123456@tcp(192.168.1.244:3306)/blog?charset=utf8&loc=Local")
	orm.RegisterModel(
		new(models.Menu),
		new(models.Users),
	)
	err := orm.RegisterDataBase("default", "mysql", "root:123456@tcp(192.168.1.244:3306)/blog?charset=utf8&loc=Local")
	fmt.Println("数据库：", err)
	orm.RunSyncdb("default", false, true)
	orm.DefaultTimeLoc = time.UTC
}

func initSession()  {
	var globalSessions *session.Manager
	sessionConfig := &session.ManagerConfig{
		CookieName: beego.AppConfig.DefaultString("sessionname", "blog"),
		EnableSetCookie: true,
		Gclifetime:beego.AppConfig.DefaultInt64("sessiongcmaxlifetime", 3600),
		Maxlifetime: beego.AppConfig.DefaultInt64("sessiongcmaxlifetime", 3600),
		Secure: false,
		CookieLifeTime: beego.AppConfig.DefaultInt("sessioncookielifetime", 3600),
		ProviderConfig: beego.AppConfig.DefaultString("sessionproviderconfig", "./tmp"),
	}

	//globalSessions, err := session.NewManager("redis", sessionConfig)
	globalSessions, err := session.NewManager(beego.AppConfig.DefaultString("sessionprovider", "memory"), sessionConfig)
	fmt.Println("GlobalSessions：", globalSessions, err)
	go globalSessions.GC()
	registerModels()
}

//注册使用session的model
func registerModels()  {
	gob.Register(&models.Users{})
}

func main() {
	if beego.BConfig.RunMode == "dev" {
		beego.BConfig.WebConfig.DirectoryIndex = true
		beego.BConfig.WebConfig.StaticDir["/swagger"] = "swagger"
	}
	beego.Run()
}
